﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace kompot_wtf
{
    public class Player : GameObject
    {
        public float MoveSpeed = 150.0f;

        private Vector2 distanceToMove;
        private Vector2 distanceMoved;

        public float Life { get; set; }

        private MobAnimation normalAnimation;
        private MobAnimation armedAnimation;

        public bool HasChainsaw { get; private set; }
        public float leftChainsawTime = 0.0f;

        public Player()
        {
            Position = new Vector2(0, 0);
            RadarStrength = INITIAL_RADAR_STRENGTH;
            CurrentRadarRadius = MAX_RADAR_RADIUS;
			Life = 100.0f;
        }

       	public float INITIAL_RADAR_STRENGTH = 1.0f;
        public float MAX_RADAR_RADIUS = 600;

        public float RadarStrength;
        public float CurrentRadarRadius;

        public void Load()
        {
			var normalTexture = WTF.ContentMgr.Load<Texture2D>("Textures//player");

			normalAnimation = new MobAnimation(normalTexture, new Vector2(40, 50));
            normalAnimation.ChangeDirection(MobAnimationDirection.Bottom);

            var armedTexture = WTF.ContentMgr.Load<Texture2D>("Textures//armed");

            armedAnimation = new MobAnimation(armedTexture, new Vector2(50, 50));
            armedAnimation.ChangeDirection(MobAnimationDirection.Bottom);

            Animation = normalAnimation;

            AnimationUpdateInterval = 0.120f;
            TimeFromLastAnimationUpdate = 0.120f;
        }

        public override void Update(float delta)
        {
            var mouseState = Mouse.GetState();
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                distanceToMove = new Vector2(mouseState.X, mouseState.Y);
                distanceToMove -= new Vector2(ScreenWidth / 2, ScreenHeight / 2);
                distanceMoved = Vector2.Zero;

                MoveDirection = Vector2.Normalize(distanceToMove) * MoveSpeed * delta;
                LookDirection = MoveDirection;

                UpdateAnimationDirection();
                Animation.Normal();
            }

            if (mouseState.RightButton == ButtonState.Pressed)
            {
                var clickPos = new Vector2(mouseState.X, mouseState.Y);
                clickPos -= new Vector2(ScreenWidth / 2, ScreenHeight / 2);
                LookDirection = Vector2.Normalize(clickPos) * MoveSpeed * delta;
                 UpdateAnimationDirection();
                Animation.Reversed();

            }

            if (distanceToMove != Vector2.Zero)
            {
                Position -= MoveDirection;
                distanceMoved += MoveDirection;

                if (distanceToMove.Length() < distanceMoved.Length())
                {
                    distanceToMove = Vector2.Zero;
                    distanceMoved = Vector2.Zero;
                }

                Sounds.play_steps();

                UpdateAnimationFrame(delta);
            }
            else
            {
                Sounds.pause_steps();
            }

            if (leftChainsawTime > 0)
            {
                leftChainsawTime -= delta;
                if (leftChainsawTime < 0)
                {
                    leftChainsawTime = 0;
                    HasChainsaw = false;
                    Animation = normalAnimation;
                    GameState.state = GameState.State.THIEF;
                    WTF.Instance.RegenerateZombies();
                }
            }

            GameObject.PlayerPosition = Position;

            const float radarStrengthDecSpeed = 0.1f;
            const float radarRadiusChangeSpeed = 250.1f;

            if (GameState.state == GameState.State.THIEF || GameState.state == GameState.State.PLAYER_ESCAPE
                || GameState.state == GameState.State.ZOMBIES_ESCAPE)
            {
                RadarStrength -= radarStrengthDecSpeed * delta;
                CurrentRadarRadius += radarRadiusChangeSpeed * delta;
                if (CurrentRadarRadius >= MAX_RADAR_RADIUS)
                {
                    WTF.Instance.ResetZombieRadarStates();

                    Sounds.play_sonar();
                }
                CurrentRadarRadius = CurrentRadarRadius % MAX_RADAR_RADIUS;
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Animation.Draw(spriteBatch, new Vector2(ScreenWidth /2, ScreenHeight / 2));
        }

        public void GiveChainsaw()
        {
            HasChainsaw = true;
            leftChainsawTime = (float)(new TimeSpan(0, 1, 9).TotalSeconds);
            armedAnimation.ChangeDirection(normalAnimation.Direction);
            Animation = armedAnimation;
        }
    }
}
        
